/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.geftu.util;

/**
 *
 * @author Nei
 */
import br.com.geftu.dao.DAO;
import br.com.geftu.model.Anexo;
import br.com.geftu.model.Relato;
import br.com.geftu.util.ValidaInsert;
import javax.swing.*;
import java.io.*;
import java.nio.channels.FileChannel;
import javax.validation.ConstraintDeclarationException;
import javax.validation.ConstraintViolationException;

public class Anexar extends JFrame {

    File arquivo = new File("");
    File destino = new File("");

//    public static void main(String[] args) {
    public void Anexar(Relato relato) {

        final JFileChooser fc = new JFileChooser();
        int res = fc.showOpenDialog(null);
        fc.setDialogTitle("Salvar arquivos");
        fc.setDialogType(JFileChooser.SAVE_DIALOG);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setMultiSelectionEnabled(true);

        if (res == JFileChooser.APPROVE_OPTION) {
            try {
                arquivo = new File(fc.getSelectedFile().getPath());
                destino = new File("d:\\teste\\" + relato.getIdrel() + "." + arquivo.getName());

                copyFile(arquivo, destino);

                insereLink(relato, destino.getAbsolutePath().toString());

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
                e.printStackTrace();
            }

        } else {
            JOptionPane.showMessageDialog(null, "Voce nao selecionou um arquivo");
        }
    }

    public static void copyFile(File source, File destination) throws IOException {

        if (destination.exists()) {
            destination.delete();
        }
        FileChannel sourceChannel = null;
        FileChannel destinationChannel = null;
        try {
            sourceChannel = new FileInputStream(source).getChannel();
            destinationChannel = new FileOutputStream(destination).getChannel();
            sourceChannel.transferTo(0, sourceChannel.size(),
                    destinationChannel);
        } finally {
            if (sourceChannel != null && sourceChannel.isOpen()) {
                sourceChannel.close();
            }
            if (destinationChannel != null && destinationChannel.isOpen()) {
                destinationChannel.close();
            }
        }
    }

    public static void insereLink(Relato relato, String caminho) {
        Anexo anexo = new Anexo();
        anexo.setRelato(relato);
        anexo.setCaminho(caminho);

        try {

            if (ValidaInsert.isvalid(anexo).isEmpty()) {
                DAO objCategoriaDefeito = new DAO(anexo);
                objCategoriaDefeito.insere();
                // preencher_jtable();
            } else {
                JOptionPane.showMessageDialog(null, ValidaInsert.isvalid(anexo));
            }
            DAO objAnexo = new DAO(anexo);

            objAnexo.insere();

        } catch (ConstraintViolationException e) {
            throw new ConstraintDeclarationException("teste" + " " + e);
            //      e.getStackTrace();
        }
    }

    public void deletarFile(File source) {
        source.delete();
        JOptionPane.showMessageDialog(null, "Arquivo Removido");
    }

    public void deletarAnexo(Anexo anexo) {
        DAO objAnexo = new DAO(anexo);
        objAnexo.excluir();
    }
}
